package com.alianzamedica.controllers.module;

import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.objectsearch.sqlsearch.ObjectSearch;
import org.objectsearch.web.tools.Converter;
import org.w3c.dom.Document;

import com.alianzamedica.businessobject.Module;
import com.alianzamedica.tools.Enviroment;

/**
 * @author Carlos
 * 
 */
public class ModuleViewAction extends Action {

	@SuppressWarnings("unchecked")
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Enviroment env = Enviroment.getInstance();
		Document doc = env.getDocument();
		ObjectSearch search = new ObjectSearch(doc,
				"com.alianzamedica.connection.ConnectionImpl");
		Integer id = Converter.string2Integer(request.getParameter("id"));
		if (id != null) {
			Module m = new Module();
			m.setId(id);
			Iterator<Module> iterator = search.searchObjects(m).iterator();
			if (iterator.hasNext()) {
				Module module = (Module) iterator.next();
				request.setAttribute("module", module);
			}
		}
		return mapping.findForward("success");
	}

}
